package p.ithorns.framework.xxljob;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * AppConfig
 * <p>
 * app-id和app-secret从基础平台获取:
 * admin登录 http://10.20.32.11:7100/
 * 从应用管理选择具体应用点开[详情]查看
 *
 * @author Ht.L
 * @date 2024-02-28 15:21
 * @since 1.0.0
 */
@Component("appContext")
@EnableConfigurationProperties(AppConf.class)
public class AppContext implements InitializingBean {

    private final static Logger log = LoggerFactory.getLogger(AppContext.class);

    /**
     * 认证
     */
    public static final String AUTHENTICATION = "Authorization";

    /**
     * 应用ID
     */
    public static final String X_APP_ID = "X-App-Id";
    /**
     * TenantId
     */
    public static final String X_TENANT_ID = "X-Tenant-Id";

//    private final AuthFeignService authService;
//
//    private final RedissonClient redis;
//
     private final AppConf appConf;
    public AppContext(AppConf appConf
//            ,AuthFeignService authService,
//                      RedissonClient redis
    ) {
        this.appConf = appConf;
//        this.authService = authService;
//        this.redis = redis;
    }

    @Override
    public void afterPropertiesSet() {
        getTokenFromAuthCenter();
    }

    public String getAppToken() {
        String token = "";
//        String token = redis.<String>getBucket(Constants.AUTH_APP_TOKEN).get();
//        if (null == token) {
//            token = getTokenFromAuthCenter();
//        }
        return token;
    }

    /**
     * 从认证中心获取token
     */
    public String getTokenFromAuthCenter() {
//        AppLoginReq req = new AppLoginReq();
//        req.setAppId(appConf.getId());
//        req.setAppSecret(appConf.getSecret());
//        req.setTenantId(appConf.getTenant());
//
//        // 调用API
//        SaResult res = authService.appLogin(req);
//        Integer code = res.getCode();
//        if (null == code || code != 200) {
//            String msg = MessageFormat.format("获取平台认证Token失败({0}): {1}", res.getCode(), res.getMsg());
//            throw new RuntimeException(msg);
//        }
//
//        JSONObject data = (JSONObject) res.getData();
//
//        // token
//        String appToken = data.getString("token");
//        // 过期时间 单位/秒
//        Integer timeout = data.getInteger("timeout");
//
//        log.info("获取AppToken: {}({})", appToken, timeout);
//
//        // 缓存
//        if (null != timeout) {
//            // 提前2分钟重新获取
//            timeout = timeout - 120;
//            if (timeout > 0) {
//                redis.<String>getBucket(Constants.AUTH_APP_TOKEN)
//                        .set(appToken, timeout, TimeUnit.SECONDS);
//            }
//        }

       //  return appToken;

        return null;
    }

    public Long getId() {
        return appConf.getId();
    }

    public String getSecret() {
        return appConf.getSecret();
    }

    public Long getTenant() {
        return appConf.getTenant();
    }

}